package com.oig.sys.oauth.handler;

import com.oig.auth.api.UserService;
import com.oig.sys.auth.dto.UserDto;
import com.oig.sys.security.support.SecurityUser;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@RequiredArgsConstructor
public class AuthenticationSuccessEventHandler extends AbstractAuthenticationSuccessEventHandler {

    private final UserService userService;

    @Override
    public void handle(Authentication authentication) {
        Object object = authentication.getPrincipal();
        if (object instanceof SecurityUser){
            log.info("user:{}, update login",object);
            SecurityUser user = (SecurityUser) authentication.getPrincipal();
            UserDto userDto = new UserDto();
            userDto.setId(user.getId());
            userDto.setUsername(user.getUsername());
            userService.updateLoginSuccess(userDto);
        } else if (object instanceof String){
            log.info("client：{} 认证成功", authentication.getPrincipal());
        }
    }

}
